<template>
  <div class="warp">
    <slot name="title" />
    <div class="handle">
      <!-- 操作按钮 -->
      <slot name="handle" :data="selectRows" />
      <!-- 自定义表头 -->
      <el-popover v-if="hasSelfHead" ref="popover" placement="bottom-end" title="" width="300" trigger="click">
        <div class="handle-content">
          <div class="title">
            <img src="@/assets/img/common/list-icon.png" class="list-icon" />
            自定义展示列
          </div>
          <div class="lists">
            <div
              v-for="(item, index) in options"
              :key="index"
              class="list-item"
              :class="{ checked: item.checked }"
              @click="handleCheck(item)"
            >
              {{ item.label }}
            </div>
            <div v-if="options.length % 2" class="list-item noCss"></div>
          </div>
          <div class="btn">
            <el-button @click="hideFun">取 消</el-button>
            <el-button type="primary" @click="confirm">确 认</el-button>
          </div>
        </div>
        <img slot="reference" src="@/assets/img/common/list.png" class="set-icon" />
      </el-popover>
    </div>
    <!-- 表格 -->
    <el-table
      v-if="columns.length > 0"
      v-loading="loading"
      :data="tableData"
      border
      stripe
      :default-expand-all="hasExpandState"
      :max-height="maxHeight"
      @selection-change="handleSelectionChange"
      @expand-change="expandChange"
    >
      <el-table-column v-if="hasExpand" type="expand" fixed="left">
        <template slot-scope="{ row }">
          <slot name="expand" :data="row" class="expandClass"></slot>
        </template>
      </el-table-column>
      <el-table-column v-if="hasSelection" type="selection" width="55" align="center" />
      <el-table-column
        v-for="(item, index) in columns"
        :key="index"
        :label="item.label"
        align="center"
        :fixed="item.fixed"
        :width="item.width"
        :show-overflow-tooltip="item.tooltip"
      >
        <!-- <template v-if="item.explain" slot="header" slot-scope="scope">
          <span>{{ item.label }}</span> <span>1234567890</span>
        </template> -->
        <template slot-scope="scope">
          <el-tooltip v-if="item.hasCopy" class="item" effect="dark" placement="right">
            <div slot="content" class="copy" @click="handleClipboard(scope.row[item.value], $event)">
              复制
            </div>
            <span v-if="item.islink" class="link2" @click="handleLink(item.value, scope.row)">{{
              scope.row[item.value]
            }}</span>
            <span v-else>{{ scope.row[item.value] }}</span>
          </el-tooltip>
          <span v-else-if="item.islink" slot="reference" class="link2" @click="handleLink(item.value, scope.row)">{{
            scope.row[item.value]
          }}</span>
          <span v-else-if="item.haspop" slot="reference">
            <div v-if="scope.row[item.value].length > 10">
              <el-popover placement="top-start" width="200" trigger="hover" :content="scope.row[item.haspop]">
                <span slot="reference">{{ scope.row[item.value] }}</span>
              </el-popover>
            </div>
            <div v-if="scope.row[item.value].length < 11">
              {{ scope.row[item.value] }}
            </div>
          </span>

          <div v-else>
            <div
              v-if="
                item.isMore &&
                  scope.row[item.value] &&
                  Array.isArray(scope.row[item.value]) &&
                  scope.row[item.value].length > 1
              "
            >
              <span v-if="item.isMoreAddTpl != true">{{ scope.row[item.value][0] }}</span>
              <el-popover placement="bottom" title="" width="300" trigger="click">
                <div v-for="(row, i) in scope.row[item.value]" :key="i">
                  {{ row }}
                </div>
                <span slot="reference" class="more">多</span>
              </el-popover>
            </div>

            <div v-else-if="item.isMore2 && scope.row[item.value] && Array.isArray(scope.row[item.value])">
              <div
                v-for="(row, i) in scope.row[item.value]"
                :key="i"
                class="link2"
                @click="handleLink(item.value, row, scope.row)"
              >
                <el-tooltip class="item" effect="dark" placement="right">
                  <div slot="content" class="copy" @click="handleClipboard(row.name, $event)">
                    复制
                  </div>
                  <span> {{ row.name }} </span>
                </el-tooltip>
              </div>
              <el-popover placement="bottom" title="" width="200" trigger="hover">
                <div
                  v-for="(row, i) in scope.row[item.value]"
                  :key="i"
                  class="link2"
                  @click="handleLink(item.value, row, scope.row)"
                >
                  {{ row.name }}
                </div>
                <span v-if="scope.row[item.value].length >= 3" slot="reference" class="link2">---</span>
              </el-popover>
            </div>

            <span v-else-if="item.isHtml" v-html="scope.row[item.value]"></span>
            <el-radio
              v-else-if="item.radio"
              v-model="radio"
              :label="scope.$index"
              @change.native="getCurrentRow(scope.row)"
            />
            <span v-else
              >{{
                item.value == '$index' ? (page.pageNum - 1) * page.pageSize + scope.$index + 1 : scope.row[item.value]
              }}
            </span>
          </div>
        </template>
      </el-table-column>
      <!-- 日志管理 -->
      <el-table-column v-if="headerExplain" align="center">
        <template slot="header" slot-scope="scope">
          <div style="display: flex; justify-content: space-between">
            <span>日志内容</span>
            <span style="color: #a2a4a8">点击日志内容查看详情</span>
          </div>
        </template>
        <template slot-scope="scope">
          <span style="cursor: pointer" @click="$emit('logClick', scope.row)">
            <div v-for="item in scope.row.logContent" :key="item" class="left">
              {{ item }}
            </div>
          </span>
        </template>
      </el-table-column>
      <!-- 表格日志 -->
      <!-- <el-table-column v-if="hasTableLog" fixed="right" label="日志" min-width="100" align="center">
        <template slot-scope="{ row, $index }">
          <slot name="table-log" :data="row" :tableIndex="$index" />
        </template>
      </el-table-column> -->
      <!-- 表格操作 -->
      <el-table-column
        v-if="hasTableHandle"
        :fixed="fixed ? 'right' : false"
        label="操作"
        :min-width="minWidth"
        align="center"
      >
        <template slot-scope="{ row, $index }">
          <slot name="table-handle" :data="row" :tableIndex="$index" />
        </template>
      </el-table-column>
    </el-table>
    <pagination v-if="columns.length > 0" :page="page" @handlePage="handlePage" />
    <div v-else class="tipsCo">
      <img src="@/assets/img/common/seleCo.png" class="tipsCo-img" />
      <div>请选择列</div>
    </div>
  </div>
</template>

<script>
import pagination from '@/components/Pagination/pagination'
import handleClipboard from '@/utils/clipboard'
export default {
  components: { pagination },
  props: {
    tableData: {
      type: Array,
      default: () => []
    },
    tableColums: {
      type: Array,
      default: () => []
    },
    maxHeight: {
      type: Number,
      default: 470
    },
    // 操作按钮的最小宽度
    minWidth: {
      type: Number,
      default: 200
    },
    // 表格名称(不可重复)
    tableName: {
      type: String,
      default: ''
    },
    // 是否有自定义列
    hasSelfHead: {
      type: Boolean,
      default: true
    },
    // 操作是否固定
    fixed: {
      type: Boolean,
      default: true
    },
    page: {
      type: Object,
      default: () => {
        return {
          total: 0
        }
      }
    },
    // 是否有选择
    hasSelection: {
      type: Boolean,
      default: false
    },
    // 表头是否有注释
    headerExplain: {
      type: Boolean,
      default: false
    },
    // 表格是否有日志
    hasTableLog: {
      type: Boolean,
      default: false
    },
    // 表格是否有操作
    hasTableHandle: {
      type: Boolean,
      default: false
    },
    // 表格是否有展开行
    hasExpand: {
      type: Boolean,
      default: false
    },
    // 是否默认展开行
    hasExpandState: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      options: [],
      columns: [],
      loading: false,
      radio: false,
      selectRows: []
    }
  },
  created() {
    this.init()
  },
  methods: {
    handleClipboard,
    init() {
      //  优先取localStorage中的数据，没有则取原始数据
      const localColumns = JSON.parse(localStorage.getItem('admin-' + this.tableName))
      //   下拉列表数据
      this.options =
        localColumns ||
        this.tableColums.map(item => {
          return {
            checked: true,
            ...item
          }
        })
      this.setTableColumns()
    },
    handleCheck(item) {
      item.checked = !item.checked
    },
    hideFun() {
      this.$refs.popover.doClose()
    },
    confirm() {
      // 本地保存数据
      localStorage.setItem('admin-' + this.tableName, JSON.stringify(this.options))
      this.setTableColumns()
      this.hideFun()
    },
    setTableColumns() {
      this.columns = this.options.filter(e => e.checked)
    },
    handleLink(column, row, baseRow) {
      this.$emit('handle-link', { column, row, baseRow })
    },
    handlePage(page) {
      this.$emit('changePage', page)
    },
    handleSelectionChange(rows) {
      this.selectRows = rows
      this.$emit('getSelectRows', rows)
    },
    expandChange(rows, aaa) {
      this.$emit('expandChange', rows)
    },
    // 单选
    getCurrentRow(row) {
      this.$emit('setRow', row)
    },
    getdata(e) {
      console.log(e)
    }
  }
}
</script>
<style lang="scss" scoped>
.warp {
  .handle {
    position: relative;
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 18px;
    .set-icon {
      cursor: pointer;
      width: 48px;
      height: 32px;
      vertical-align: bottom;
    }
    .set-icon:hover {
      opacity: 0.7;
    }
  }
}
// 自定义列
.handle-content {
  .title {
    padding-left: 10px;
  }
  .list-icon {
    vertical-align: text-bottom;
    margin-right: 5px;
  }
  .lists {
    margin-top: 5px;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    max-height: 300px;
    overflow: auto;
    // box-shadow: 0px 3px 1px #f4f4f4;
    .list-item {
      margin: 8px 0;
      width: 42%;
      text-align: center;
      padding: 6px 0;
      background: #f5f7fa;
      border: 1px solid #f5f7fa;
      cursor: pointer;
    }
    .noCss {
      background: none;
      border: none;
    }
    .list-item:hover {
      color: #4064df;
    }
    .checked {
      background: url('~@/assets/img/common/listItemBg.png') no-repeat;
      background-size: 100% 99%;
      color: #4064df;
    }
    // color: red;
  }
  .lists::-webkit-scrollbar-track {
    background-color: #f5f7fa;
  }

  .lists::-webkit-scrollbar {
    width: 7px;
    height: 8px;
  }

  .lists::-webkit-scrollbar-thumb {
    background-color: #ccc;
    border-radius: 5px;
  }
  .btn {
    text-align: center;
    margin-top: 10px;
  }
}
.link2 {
  color: #284ed7;
  cursor: pointer;
}
.more {
  background: #ff5722;
  padding: 4px 5px;
  border-radius: 4px;
  color: #fefefe;
  cursor: pointer;
  margin-left: 4px;
}
.copy {
  cursor: pointer;
}
.tipsCo {
  text-align: center;
  color: #767676;
  margin-top: 100px;
  .tipsCo-img {
    width: 150px;
    opacity: 0.7;
    height: auto;
  }
}
.body-title {
  padding-bottom: 10px;
  margin-bottom: 20px;
  border-bottom: 1px solid #999999;
}

.el-table {
  .el-table__fixed,
  .el-table__fixed-right {
    height: 100% !important;
    right: 0px !important;
  }
  &.el-table--fluid-height {
    &.el-table--scrollable-y {
      .el-table__fixed-body-wrapper {
        bottom: 1px;
        max-height: none !important;
      }
      &.el-table--scrollable-x {
        .el-table__fixed-body-wrapper {
          bottom: 8px;
          max-height: none !important;
        }
      }
    }
  }
  .el-radio__label {
    display: none;
  }
}

.warp .el-table__body-wrapper {
  .el-table__expanded-cell {
    z-index: 100;
    padding: 0;
    background: #f4f4f4;
    box-shadow: 1px 1px 7px #dfdfdf inset;
  }
  .el-table__expanded-cell:hover {
    background: #f3f3f3 !important;
  }
}
.warp .el-table__fixed,
.warp.el-table__fixed-right {
  .el-table__expanded-cell {
    visibility: hidden;
    padding: 0;
    // background: #ccc;
  }
  .el-table__expanded-cell:hover {
    visibility: hidden;
    padding: 0;
    // background: #ccc;
  }
}
.left {
  text-align: left;
}
</style>
